package my.jwds.p49;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroupAnagrams {

    public static void main(String[] args) {
        GroupAnagrams obj = new GroupAnagrams();
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> res = new HashMap<>();
        for (String str : strs) {
            Map<Character,Integer> key = new HashMap<>();
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                key.putIfAbsent(c,0);
                key.put(c,key.get(c)+1);
            }
            res.putIfAbsent(key.toString(),new ArrayList<>());
            res.get(key.toString()).add(str);
        }
        return new ArrayList<>(res.values());
    }
}